package com.lei.study.lock;

import java.util.concurrent.CountDownLatch;

/**
 * 测试
 *
 * @author 伍磊
 */
public class ZkLockTest implements Runnable{

    public static int stock = 3;

    public static int num = 5;

    public static CountDownLatch countDownLatch = new CountDownLatch(num);

    public static void main(String[] args) {
        for (int i = 0; i < num; i++) {
            new Thread(new ZkLockTest()).start();
            countDownLatch.countDown();
        }
    }

    @Override
    public void run() {
        try {
            // 加锁
            ZkLock zkLock = new ZkLock();
            countDownLatch.await();
            zkLock.lock();
            if (stock > 0) {
                zkLock.lock();
                System.out.println("******* 抢到了，剩余库存: " + --stock);
                zkLock.unLock();
            }
            zkLock.unLock();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
